package com.leoandroid.tool.toolsbox

import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.Gson
import com.leoandroid.tool.toolsbox.bean.WanNianLi
import com.leoandroid.tool.toolsbox.largeread.databinding.ActivityWannianliBinding
import com.leoandroid.tool.toolsbox.net.NetCallBack
import com.leoandroid.tool.toolsbox.net.NetRequest
import com.leoandroid.tool.toolsbox.utils.TimeUtils
import org.json.JSONObject
import java.text.SimpleDateFormat
import java.util.*

class WanNianLiActivity : AppCompatActivity() {

    var bindingF: ActivityWannianliBinding? = null
    val binding get() = bindingF!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bindingF = ActivityWannianliBinding.inflate(layoutInflater)
        setContentView(binding.root)
        if (com.leoandroid.tool.toolsbox.largeread.utils.ToolUtils.getHeightStatusBar(this) > 0) {
            binding.includeTitle.layoutPadding.setPadding(
                0,
                com.leoandroid.tool.toolsbox.largeread.utils.ToolUtils.getHeightStatusBar(this),
                0,
                0
            )
        }
        com.leoandroid.tool.toolsbox.largeread.utils.ToolUtils.setStatusBarHomeTransparent(this)
        binding.includeTitle.backIv.setOnClickListener {
            finish()
        }
        val time = SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis())
        binding.includeTitle.titleTv.text = time
        binding.dayOfDateTv.text = Calendar.getInstance().get(Calendar.DAY_OF_MONTH).toString()
        val url =
            "http://v.juhe.cn/calendar/day?date=${TimeUtils.getCurrentDateTime()}&key=c1be47d9bbcb5e4e51fb6f4fe891f592"
        NetRequest.asyncGet(url, object : NetCallBack {
            override fun onFailure(code: Int, e: Throwable?) {

            }

            override fun onSuccess(json: JSONObject?) {
                Log.d("xxxxxxx", "json>>" + json.toString())
                val result = json?.getJSONObject("result")
                val data = result?.getJSONObject("data")
                val wannianli = Gson().fromJson(data.toString(), WanNianLi::class.java)
                runOnUiThread {
                    if (!TextUtils.isEmpty(wannianli.holiday)) {
                        binding.holidayTv.visibility = View.VISIBLE
                    } else {
                        binding.holidayTv.visibility = View.GONE
                    }
                    binding.avoidTv.text = wannianli.avoid
                    binding.animalsYearTv.text = "属相：" + wannianli.animalsYear
                    if (!TextUtils.isEmpty(wannianli.desc)) {
                        binding.descTv.text = "假日描述：" + wannianli.desc
                        binding.dateTv.visibility = View.VISIBLE
                    } else {
                        binding.dateTv.visibility = View.GONE
                    }
                    binding.weekdayTv.text = "星期：" + wannianli.weekday
                    binding.suitTv.text = wannianli.suit
                    binding.lunarYearTv.text = "纪年：" + wannianli.lunarYear + " " + wannianli.lunar
                }
            }
        })
    }
}